package cn.agiledata.bank.linkbank.impl.hxb;

import java.math.BigDecimal;
import java.util.Date;

import cn.agiledata.bank.common.util.DateUtil;
import cn.agiledata.bank.linkbank.exception.LinkBankException;
import cn.agiledata.bank.linkbank.impl.AbstractBankNetPayRequest;
import cn.agiledata.bank.linkbank.impl.AbstractBankPayRequest;
import cn.agiledata.bank.linkbank.logic.DigitalSignGeneratable;

/**
 * 华夏银行网上银行支付请求
 * 
 * @author jessik
 * 
 */
public class HxbNetPayRequest extends AbstractBankNetPayRequest implements
        DigitalSignGeneratable {
    private static final String[] REQUEST_KEYS = { HxbNetConstant.ORDER_NO,
            HxbNetConstant.MERCHANT_ID, HxbNetConstant.TERM_NO,
            HxbNetConstant.MER_IP, HxbNetConstant.TX_AMT,
            HxbNetConstant.CURY_TYPE, HxbNetConstant.TX_DATE,
            HxbNetConstant.TX_TIME, HxbNetConstant.RET_URL };

    private String iniFilePath;

    public HxbNetPayRequest() {
        super();
        init();
    }

    protected AbstractBankPayRequest getCopiedInstance() {
        HxbNetPayRequest request = new HxbNetPayRequest();
        request.iniFilePath = this.iniFilePath;
        return request;
    }

    private void init() {
        this.setParameter(HxbNetConstant.CURY_TYPE,
                HxbNetConstant.CURY_TYPE_CNY);
    }

    public void setAmount(BigDecimal amount) {
        this.setParameter(HxbNetConstant.TX_AMT, amount.multiply(
                HxbNetConstant.HUNDRED).setScale(0).toString());
    }

    public void setDate(Date date) {
        String dateValue = DateUtil.date2String(date,
                HxbNetConstant.TX_DATE_REQUEST_FORMAT);
        setParameter(HxbNetConstant.TX_DATE, dateValue);
        String timeValue = DateUtil.date2String(date,
                HxbNetConstant.TX_TIME_REQUEST_FORMAT);
        setParameter(HxbNetConstant.TX_TIME, timeValue);
    }

    public String generateDigitalSign(String cryptoKey)
            throws LinkBankException {
        String[] keys = REQUEST_KEYS;
        String digitalSign = HxbUtils.getDigitalSign(this.getParameters(),
                keys, this.iniFilePath);
        this.setParameter(HxbNetConstant.ORDER_INFO, digitalSign);
        return digitalSign;
    }

    public String getMerchantUrl() {
        return getParameter(HxbNetConstant.RET_URL);
    }

    public void setMerchantUrl(String merchantUrl) {
        this.setParameter(HxbNetConstant.RET_URL, merchantUrl);
    }

    public String getMerchantId() {
        return this.getParameter(HxbNetConstant.MERCHANT_ID);
    }

    public void setMerchantId(String merchantId) {
        this.setParameter(HxbNetConstant.MERCHANT_ID, merchantId);
    }

    public String getCryptoKeyDescription() {
        return "";
    }

    public boolean validateCryptoKey(String cryptoKey) {
        return true;
    }

    public String getIniFilePath() {
        return iniFilePath;
    }

    public void setIniFilePath(String iniFilePath) {
        this.iniFilePath = iniFilePath;
    }

    /**
     * 得到商家IP
     * 
     * @return
     */
    public String getMerIp() {
        return this.getParameter(HxbNetConstant.MER_IP);
    }

    /**
     * 设置商家IP
     * 
     * @param merIp
     */
    public void setMerIp(String merIp) {
        this.setParameter(HxbNetConstant.MER_IP, merIp);
    }

    /**
     * 得到终端号
     * 
     * @return
     */
    public String getTermNo() {
        return this.getParameter(HxbNetConstant.TERM_NO);
    }

    /**
     * 设置终端号
     * 
     * @param termNo
     */
    public void setTermNo(String termNo) {
        this.setParameter(HxbNetConstant.TERM_NO, termNo);
    }

    public void setOrderId(String orderId) {
        this.setParameter(HxbNetConstant.ORDER_NO, orderId);
    }

    public String getOrderId() {
        return this.getParameter(HxbNetConstant.ORDER_NO);
    }

}
